﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TRPOP.CRM.Component.Model;
using TRPOP.CRM.Component.DALSQLServer;
using TRPOP.Common;

namespace TRPOP.CRM.Component.Business
{
    /// <summary>
    /// 权限管理
    /// </summary>
    public class PermissionManage
    {
        /// <summary>
        /// 删除 权限组
        /// </summary>
        /// <param name="intiSystemRoleID"></param>
        /// <returns></returns>
        public static bool DeleteSystemRole(int intiSystemRoleID)
        {
            return new SystemRoleDAL().DeleteInfoById(intiSystemRoleID);
        }

        /// <summary>
        /// 根据ID获取 权限组 信息
        /// </summary>
        /// <param name="intiSystemRoleID"></param>
        /// <returns></returns>
        public static SystemRoleInfo GetSystemRoleInfoById(int intiSystemRoleID)
        {
            return new SystemRoleDAL().GetInfoById(intiSystemRoleID);
        }

        /// <summary>
        /// 获取全部的 权限组 信息
        /// </summary>
        /// <returns></returns>
        public static List<SystemRoleInfo> GetAllSystemRoleInfo()
        {
            return new SystemRoleDAL().GetListByCondition(0, string.Empty, string.Empty);
        }

        /// <summary>
        /// 根据角色ID 获取 权限角色信息
        /// </summary>
        /// <param name="intiSystemRoleID"></param>
        /// <returns></returns>
        public static List<RolePermissionInfo> GetRolePermissionInfoByRoleID(int intiSystemRoleID)
        {
            return new RolePermissionDAL().GetListByCondition(0, " AND iSystemRoleID=" + intiSystemRoleID, string.Empty);
        }

        /// <summary>
        /// 获取所有的 权限信息
        /// </summary>
        /// <returns></returns>
        public static List<PermissionInfo> GetAllPermissionInfo()
        {
            return new PermissionDAL().GetListByCondition(0, string.Empty, " iPermissionID ASC");
        }

        /// <summary>
        /// 保存权限组信息
        /// </summary>
        /// <param name="oSystemRoleInfo"></param>
        /// <param name="intiPermissionIDs"></param>
        /// <returns></returns>
        public static bool SaveSysRole(SystemRoleInfo oSystemRoleInfo, List<int> intiPermissionIDs)
        {
            bool blnReturn = false;

            blnReturn = new SystemRoleDAL().Save(oSystemRoleInfo);

            if (blnReturn && intiPermissionIDs.Count <= 0)
            {
                return blnReturn;
            }
            else if (blnReturn && intiPermissionIDs.Count > 0)
            {
                blnReturn = new RolePermissionDAL().SaveRolePermission(intiPermissionIDs, oSystemRoleInfo.iSystemRoleID);
            }

            return blnReturn;

        }

        /// <summary>
        /// 根据员工ID获取员工所属系统角色信息
        /// </summary>
        /// <param name="intiEmployeeID"></param>
        /// <returns></returns>
        public static EmployeeRoleInfo GetEmployeeRoleInfo(int intiEmployeeID)
        {
            return new EmployeeRoleDAL().GetInfoById(intiEmployeeID);
        }

        /// <summary>
        /// 根据员工ID获取员工 所有的权限信息
        /// </summary>
        /// <param name="intiEmployeeID"></param>
        /// <returns></returns>
        public static List<PermissionInfo> GetPermissionInfoByEmployeeID(int intiEmployeeID)
        {
            List<PermissionInfo> oList = new List<PermissionInfo>();

            EmployeeRoleInfo oEmployeeRoleInfo = GetEmployeeRoleInfo(intiEmployeeID);

            if (oEmployeeRoleInfo != null)
            {
                string strCondation = string.Format(" AND iPermissionID IN ( SELECT iPermissionID FROM RolePermission WHERE iSystemRoleID={0} ) ", oEmployeeRoleInfo.iSystemRoleID);

                oList = new PermissionDAL().GetListByCondition(0, strCondation, string.Empty);

                if (oList == null)
                {
                    oList = new List<PermissionInfo>();
                }
            }

            return oList;
        }

        /// <summary>
        /// 保存员工的权限信息
        /// </summary>
        /// <param name="intiEmployeeID"></param>
        /// <param name="intiSystemRoleID"></param>
        public static bool SaveEmployeeRoleInfo(int intiEmployeeID, int intiSystemRoleID)
        {
            EmployeeRoleInfo oEmployeeRoleInfo = new EmployeeRoleInfo();

            oEmployeeRoleInfo.iEmployeeID = intiEmployeeID;
            oEmployeeRoleInfo.iSystemRoleID = intiSystemRoleID;

            return new EmployeeRoleDAL().Save(oEmployeeRoleInfo);
        }
    }
}
